package com.wzdigit.wms.raw.controller.inv;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.basic.client.request.inv.SearchGoodsConverInfoRequest;
import com.wzdigit.wms.basic.client.request.inv.SubmitGoodsConverRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.inv.GoodsConverInfoResponse;
import com.wzdigit.wms.common.utils.ValidatorUtil;
import com.wzdigit.wms.raw.constant.RawWareConstant;
import com.wzdigit.wms.raw.service.inv.GoodsConverService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * 料号转换
 * @author Administrator
 *
 */
@RestController
@Api(tags = "料号转换")
@RequestMapping(value = RawWareConstant.API_VERSION)
public class GoodsConverController {
	
	public static final String REQUEST_MODEL = "/goodsconver";

    @Autowired
    private GoodsConverService goodsConverService;

    @ApiOperation(value = "扫描SN", notes = "扫描SN")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/scanInfo")
    public GoodsConverInfoResponse scanInfo(
    		SearchGoodsConverInfoRequest dto,
            HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
    	try {
    		return goodsConverService.scanInfo(dto);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    @ApiOperation(value = "提交",notes = "提交")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @PostMapping(value = REQUEST_MODEL + "/submit")
    public Result<?> submit(
    		@RequestBody @Valid SubmitGoodsConverRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) throws Exception {
        try {
        	ValidatorUtil.validator(bindingResult);
            return goodsConverService.submit(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
}